
<div class=" layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
        <a lay-href="">首页</a>
        <a>商户管理</a>
        <a><cite>支付渠道</cite></a>
    </div>
</div>

<style>
    .layui-form-label {
        width:120px !important;
        margin-left:1%;
    }
    .layui-input-inline {
        width: 80% !important;
    }
    .layui-form-radio{
        padding-right: 18px;
    }
</style>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-tab layui-tab-brief">
            <ul class="layui-tab-title">
                <li class="layui-this">渠道配置</li>
            </ul>
            <div class="layui-tab-content">
                <form class="layui-form center">
                    <input type="hidden" id="id" name="id">
                    <input type="hidden" id="productId" name="productId">
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="productName" id="productName" style="border-width: 0px;" required lay-verify="required" autocomplete="off" class="layui-input" readonly="readonly" disabled="disabled">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">产品类型</label>
                        <div class="layui-input-inline" >
                            <input type="radio" name="productType" title="收款" value="1" disabled />
                            <input type="radio" name="productType" title="充值" value="2" disabled />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">代理商费率(%)</label>
                        <div class="layui-input-inline">
                            <input type="text" name="agentRate" id="agentRate" style="border-width: 0px;" required lay-verify="required" autocomplete="off" class="layui-input" readonly="readonly" disabled="disabled">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">商户费率(%)</label>
                        <div class="layui-input-inline">
                            <input type="text" id="mchRate" name="mchRate"  placeholder="请输入商户费率" lay-verify="required|number" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">接口模式</label>
                        <div class="layui-input-inline" >
                            <input type="radio" name="ifMode" title="单独" value="1" lay-filter="ifModeFilter" checked="checked"/>
                            <input type="radio" name="ifMode" title="轮询" value="2" lay-filter="ifModeFilter"/>
                        </div>
                    </div>

                    <div class="layui-form-item" id="payPassageIdDiv" style="display: none">
                        <div class="layui-form-item">
                            <label class="layui-form-label">支付通道</label>
                            <div class="layui-input-inline" >
                                <select name="payPassageId" id="payPassageId" lay-filter="payPassageFilter">
                                    <option value="">选择支付通道</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">通道子账户</label>
                            <div class="layui-input-inline" >
                                <select name="payPassageAccountId" id="payPassageAccountId" lay-filter="payPassageAccountFilter">
                                    <option value="">选择通道子账户</option>
                                </select>
                            </div>
                        </div>
                    </div>

                    <div class="layui-form-item" id="pollWeightDiv" style="display: none">
                        <label class="layui-form-label">支付通道</label>
                        <div class="layui-input-inline" >
                            <table id="XxPay_Mgr_Poll_dataAll" lay-filter="XxPay_Mgr_Poll_dataAll"></table>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">子账户轮询策略</label>
                        <div class="layui-input-inline" >
                            <input type="radio" name="accountStrategy" title="按下单时间轮询" value="1" checked="checked"/><i id="accountStrate1" class="layui-icon layui-icon-about" style="font-size: 12px;margin-left:-25px;position: absolute;top: 14px"></i>
                            <input type="radio" name="accountStrategy" title="按订单笔数轮询" value="2"/><i id="accountStrate2" class="layui-icon layui-icon-about" style="font-size: 12px;margin-left:-25px;position: absolute;top: 14px"></i>
                            <input type="radio" name="accountStrategy" title="按金额轮询" value="3"/><i id="accountStrate3" class="layui-icon layui-icon-about" style="font-size: 12px;margin-left:-25px;position: absolute;top: 14px"></i>
                            <input type="radio" name="accountStrategy" title="随机轮询" value="4"/><i id="accountStrate4" class="layui-icon layui-icon-about" style="font-size: 12px;margin-left:-25px;position: absolute;top: 14px"></i>
                        </div>
                    </div>

                    <div class="layui-form-item">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline" >
                            <input type="radio" name="status" title="开启" value="1" />
                            <input type="radio" name="status" title="关闭" value="0" />
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-inline">
                            <button type="button" class="layui-btn" lay-submit="" lay-filter="update">保存</button>
                            <button type="button" id="btnBack" class="layui-btn">返回</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script>

    layui.use(['form','table','util','admin',],function(){
        var form = layui.form
                , $ = layui.$
                , admin = layui.admin
                , layer = layui.layer
                , element = layui.element
                , table = layui.table;

        element.render('breadcrumb', 'breadcrumb'); //渲染导航信息

        var router = layui.router();
        var productId = router.search.productId;

        admin.req({
            type: 'post',
            url: layui.setter.baseUrl + '/mch_pay_passage_set/get',
            data: {
                productId: productId
            },
            error: function(err){
                layer.alert(err);
            },
            success: function(res){
                if(res.code == 0){
                    $('#id').val(res.data.id);
                    $('#productId').val(res.data.productId);
                    $('#productName').val(res.data.productName);
                    $("input[name='productType'][value='"+res.data.productType+"']").attr("checked",true);
                    if(res.data.agentRate == '' || res.data.agentRate == null) {
                        $('#agentRate').val('未设置');
                    }else {
                        $('#agentRate').val(res.data.agentRate);
                    }
                    $('#mchRate').val(res.data.mchRate);
                    $("input[name='status'][value='"+res.data.status+"']").attr("checked",true);
                    if(res.data.ifMode == 1) {
                        $("input[name='ifMode'][value='1']").attr("checked",true);
                        $("#payPassageIdDiv").show();
                        $("#pollWeightDiv").hide();
                    }else if(res.data.ifMode == 2) {
                        $("input[name='ifMode'][value='2']").attr("checked",true);
                        $("#payPassageIdDiv").hide();
                        $("#pollWeightDiv").show();
                    }

                    $("input[name='accountStrategy'][value='"+res.data.accountStrategy+"']").attr("checked", true);

                    var payPassageId = res.data.payPassageId;
                    // 加载支付通道列表
                    admin.req({
                        type: 'post',
                        url: layui.setter.baseUrl + '/config/common/pay_passage_product',
                        data: {productId: productId},
                        error: function(err){
                            layer.alert(err);
                        },
                        success: function(res){
                            if(res.code == 0){
                                var payPassageList = res.data;
                                //遍历赋值
                                for(var i in payPassageList){
                                    $("#payPassageId").append('<option value= '+payPassageList[i].id+'>'+payPassageList[i].passageName+' [' + payPassageList[i].passageRate + '%]' +'</option>');
                                }
                                $("#payPassageId").find('option[value="'+payPassageId+'"]').attr('selected', true);
                                form.render('select');
                            }
                        }
                    });

                    var payPassageAccountId = res.data.payPassageAccountId;
                    // 加载支付通道子账户列表
                    admin.req({
                        type: 'post',
                        url: layui.setter.baseUrl + '/config/common/pay_passage_account',
                        data: {payPassageId: payPassageId},
                        error: function(err){
                            layer.alert(err);
                        },
                        success: function(res){
                            if(res.code == 0){
                                var payPassageAccountList = res.data;
                                //遍历赋值
                                for(var i in payPassageAccountList){
                                    $("#payPassageAccountId").append('<option value= '+payPassageAccountList[i].id+'>'+payPassageAccountList[i].accountName+' [' + payPassageAccountList[i].id + ']' +'</option>');
                                }
                                $("#payPassageAccountId").find('option[value="'+payPassageAccountId+'"]').attr('selected', true);
                                form.render('select');
                            }
                        }
                    });
                    // 监控通道列表下拉选择事件
                    form.on('select(payPassageFilter)', function(data){
                        var payPassageId = data.value;
                        // 清除旧的
                        $("#payPassageAccountId option").each(function(){  //遍历所有option
                            var val = $(this).val();   //获取option值
                            if(val != '') {
                                this.remove();
                            }
                        });
                        admin.req({
                            type: 'post',
                            url: layui.setter.baseUrl + '/config/common/pay_passage_account',
                            data: {payPassageId: payPassageId},
                            error: function(err){
                                layer.alert(err);
                            },
                            success: function(res){
                                if(res.code == 0){
                                    var payPassageAccountList = res.data;
                                    //遍历赋值
                                    for(var i in payPassageAccountList){
                                        $("#payPassageAccountId").append('<option value= '+payPassageAccountList[i].id+'>'+payPassageAccountList[i].accountName+' [' + payPassageAccountList[i].id + ']' +'</option>');
                                    }
                                    form.render('select');
                                }
                            }
                        });
                    });

                    // 通道轮询
                    table.render({
                        elem: '#XxPay_Mgr_Poll_dataAll'
                        ,url: layui.setter.baseUrl + '/mch_pay_passage_set/poll_get' //
                        ,where: {
                            access_token: layui.data(layui.setter.tableName).access_token,
                            productId: productId
                        }
                        ,id: 'tableReload'
                        ,page: false
                        ,cols: [[
                            {field: 'ok', type: 'checkbox', fixed: 'left'}
                            ,{field: 'id', title: '通道ID', width: 180}
                            ,{field: 'passageName', title: '通道名称', width: 250}
                            ,{field: 'weight', edit: 'text', title: '轮询权重(1-9)', width: 240}
                        ]]
                        ,skin: 'line'
                        ,done: function(res, curr, count){}
                    });

                    form.render();
                }
            }
        })

        form.on('submit(update)', function(data){
            var agentRate = $.trim($('#agentRate').val());
            var mchRate = $.trim($('#mchRate').val());
            var agentId = $.trim($('#agentId').val());
            console.log("agentRate:" + agentRate + ",mchRate:" + mchRate);
            // 如果商户有代理,则需与代理商费率对比判断
            if(agentId != '') {
                if(agentRate == '' || agentRate == '未设置') {
                    layer.alert("请先去代理商管理中配置代理商费率",{title: '提示'});
                    return false;
                }
                if(agentRate/1 > mchRate/1) {
                    layer.alert("代理商费率不能大于商户费率",{title: '提示'});
                    return false;
                }
            }

            var payPassageId = $.trim($('#payPassageId').val());
            var ifMode = $('input[name="ifMode"]:checked ').val();
            if(ifMode == 1 && payPassageId == '') {
                layer.alert("请选择支付通道",{title: '提示'});
                return false;
            }

            // 获取表格选中行的数据
            var checkDatas = table.checkStatus('tableReload');
            if(ifMode == 2 && checkDatas.data.length <= 0) {
                layer.alert("请选择支付通道",{title: '提示'});
                return false;
            }
            //console.log(checkStatus.checkDatas);
            var poll = [];
            $.each(checkDatas.data, function(index, val) {
                console.log(val.id + "," + val.weight);

                var p = {};
                p.payPassageId = val.id;
                p.weight = val.weight;
                poll.push(p);

            } );
            // 转成轮询参数json
            var pollParam = JSON.stringify(poll);
            data.field.pollParam = pollParam;

            admin.req({
                type: 'post',
                url: layui.setter.baseUrl + '/mch_pay_passage_set/update',
                data: data.field,
                error: function(err){layer.alert(err.msg,{title:"请求失败"})},
                success: function(res){
                    if(res.code == 0){
                        layer.alert(res.msg, {title: '保存成功'},function(index){
                            layer.close(index);
                            location.hash = '/pay_config/pay_passage_set/';
                        });
                    }
                }
            })
            return false;
        });

        $('#btnBack').on('click', function() {
            location.hash = '/pay_config/pay_passage_set/';
        });

        form.on('radio(ifModeFilter)', function(data){
            if(data.value == 1) {
                $("#payPassageIdDiv").show();
                $("#pollWeightDiv").hide();
            }else if(data.value == 2) {
                $("#payPassageIdDiv").hide();
                $("#pollWeightDiv").show();
            }
        });

        $("#accountStrate1").mouseover(function () {
            layer.tips('当天子账号最早下单(订单状态为支付中、成功、处理完成)优先', '#accountStrate1', {
                tips: [1, '#3595CC'],
            });
        });

        $("#accountStrate2").mouseover(function () {
            layer.tips('当天子账号下单数量(订单状态支付中、成功、处理完成)最少的优先', '#accountStrate2', {
                tips: [1, '#3595CC']
            });
        });

        $("#accountStrate3").mouseover(function () {
            layer.tips('当天子账号订单金额(订单状态为成功或处理完成)少的优先', '#accountStrate3', {
                tips: [1, '#3595CC']
            });
        });

        $("#accountStrate4").mouseover(function () {
            layer.tips('随机取', '#accountStrate4', {
                tips: [1, '#3595CC']
            });
        });

    });

</script>
